package com.okhttp.comment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baselibrary.BaseConstant;
import com.baselibrary.bean.CommentEnum;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.ARouterUtils;
import com.baselibrary.utils.CommonUtil;
import com.baselibrary.utils.SpUtils;
import com.baselibrary.utils.ToastUtils;
import com.like.LikeButton;
import com.okhttp.SendRequest;
import com.okhttp.callbacks.GenericsCallback;
import com.okhttp.sample_okhttp.JsonGenericsSerializator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * @author 关忆北丶
 * @description:
 * @date :2021/3/31 14:10
 */
public class IPCommentListener implements OnCommentClickListener {

    private Activity mActivity;
    private BaseCommentView mBaseCommentView;

    private BaseCommentChildListAdapter commentChildListAdapter;
    private int pos = -1;
    private int commentType = -1;
    private int onClickCommentType = -1;
    private BaseCommentAdapter mCommentAdapterType;
    private String content = "";

    public void setContent(String content) {
        this.content = content;
    }

    public void setCommentAdapterType(BaseCommentAdapter mCommentAdapterType) {
        this.mCommentAdapterType = mCommentAdapterType;
    }

    public int getPos() {
        return pos;
    }

    public BaseCommentChildListAdapter getCommentChildListAdapter() {
        return commentChildListAdapter;
    }

    public IPCommentListener(Activity mActivity, BaseCommentView mBaseCommentView) {
        this.mActivity = mActivity;
        this.mBaseCommentView = mBaseCommentView;
    }

    @Override
    public void onUserHead(Object norMarListBean) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (norMarListBean instanceof CommentBean.DataBean.CommentInfoBean) {
            hashMap.put(BaseConstant.USER_ID, String.valueOf(((CommentBean.DataBean.CommentInfoBean) norMarListBean).getUser().getId()));
        } else {
            if (norMarListBean instanceof CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean) {
                hashMap.put(BaseConstant.USER_ID, String.valueOf(((CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean) norMarListBean).getUser().getId()));
            }
        }
        ARouterUtils.jump(ActivityRouter.MINE_CENTER.USER_INFO, hashMap);
    }

    @Override
    public void onCommentView(CommentBean.DataBean.CommentInfoBean norMarListBean, int pos) {
        if (this.pos != pos || onClickCommentType != 1) {
            //不是同一个点击条目 对评论内容进行清空
            content = "";
        }
        this.pos = pos;
        onClickCommentType = 1;
        Intent intent = new Intent(mActivity, BusinessLayout.mCommentHelp.getWindowAddComment());
        Bundle bundle = new Bundle();
        bundle.putInt("type", 1);
        bundle.putBoolean("isShowReply", true);
        bundle.putInt("typeId", norMarListBean.getId());
        bundle.putInt("pId", norMarListBean.getId());
        bundle.putInt("puId", norMarListBean.getUser().getId());
        bundle.putSerializable("dataBean", norMarListBean);
        bundle.putSerializable("content", content);
        intent.putExtras(bundle);
        mActivity.startActivityForResult(intent, CommentConstant.COMMENT_REQUESTCODE);
    }

    /**
     * 对子评论进行评论
     *
     * @param norMarListBean
     * @param replyCommentsBean
     * @param commentChildListAdapter
     * @param pos
     */
    @Override
    public void onCommentChildClick(CommentBean.DataBean.CommentInfoBean norMarListBean, CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean, BaseCommentChildListAdapter commentChildListAdapter, int pos) {
        this.commentChildListAdapter = commentChildListAdapter;
        if (this.pos != pos || onClickCommentType != 2) {
            //不是同一个点击条目 对评论内容进行清空
            content = "";
        }
        this.pos = pos;
        onClickCommentType = 2;
        Intent intent = new Intent(mActivity, BusinessLayout.mCommentHelp.getWindowAddComment());
        Bundle bundle = new Bundle();
        bundle.putInt("type", 1);
        bundle.putBoolean("isShowReply", true);
        bundle.putInt("typeId", replyCommentsBean.getId());
        bundle.putInt("pId", norMarListBean.getId());
        bundle.putInt("puId", replyCommentsBean.getUser().getId());
        bundle.putSerializable("content", content);
        bundle.putSerializable("dataBean", replyCommentsBean);
        intent.putExtras(bundle);
        mActivity.startActivityForResult(intent, CommentConstant.COMMENT_REQUESTCODE);
    }

    /**
     * 删除子评论
     *
     * @param replyCommentsBean
     * @param commentChildListAdapter
     * @param mTvCommentReplyNum
     * @param childPos                子评论pos
     * @param pos
     */
    @Override
    public void onDeleteReplyComment(CommentBean.DataBean.CommentInfoBean norMarListBean, CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean, BaseCommentChildListAdapter commentChildListAdapter, TextView mTvCommentReplyNum, int childPos, int pos) {
        BusinessLayout.webCommentDel(mActivity, replyCommentsBean.getId(), new BusinessLayout.OnDeleteCommentListener() {
            @Override
            public void onSuccess() {
                norMarListBean.getReplyComments().remove(replyCommentsBean);
                commentChildListAdapter.removeData(replyCommentsBean);
                norMarListBean.setReplyCount(norMarListBean.getReplyCount() - 1);
                if (norMarListBean.getReplyCount() > 2) {
                    mTvCommentReplyNum.setVisibility(View.VISIBLE);
                    mTvCommentReplyNum.setText("展开" + norMarListBean.getReplyCount() + "条回复");
                } else {
                    mTvCommentReplyNum.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFail() {
                ToastUtils.showShort(mActivity, "删除失败");
            }
        });

    }

    @Override
    public void onGiveReplyComment(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean, TextView mSupportNum, LikeButton ivCommentGive) {
        //点赞
        mBaseCommentView.giveClick(replyCommentsBean, mSupportNum, ivCommentGive, () -> {
        });
    }

    /**
     * 删除主评论
     *
     * @param norMarListBean
     */
    @Override
    public void onDeleteComment(CommentBean.DataBean.CommentInfoBean norMarListBean) {

        BusinessLayout.webCommentDel(mActivity, norMarListBean.getId(), new BusinessLayout.OnDeleteCommentListener() {
            @Override
            public void onSuccess() {
                mBaseCommentView.removeData(norMarListBean);
                List<CommentBean.DataBean.CommentInfoBean> data = mBaseCommentView.getData();
                if (data == null || data.size() == 0) mBaseCommentView.isEmptyView(false);

            }

            @Override
            public void onFail() {
                ToastUtils.showShort(mActivity, "删除失败");
            }
        });

    }

    /**
     * 主评论点赞
     *
     * @param norMarListBean
     * @param mIvCommentGive
     * @param mTvCommentGiveNum
     */
    @Override
    public void onGiveComment(CommentBean.DataBean.CommentInfoBean norMarListBean, LikeButton mIvCommentGive, TextView mTvCommentGiveNum) {
        //点赞
        mBaseCommentView.giveClick(norMarListBean.getId(), mIvCommentGive, mTvCommentGiveNum, norMarListBean, () -> {
        });
    }

    /**
     * 查看全部X条回复
     *
     * @param norMarListBean
     */
    @Override
    public void onLookCommentDetails(CommentBean.DataBean.CommentInfoBean norMarListBean, BaseCommentChildListAdapter adapter, ProgressBar progressBar, OnLookSurplusCommentListener listener) {
        loadCommentMoreDetails(norMarListBean, adapter, progressBar, listener);
    }

    @Override
    public void onReportComment(CommentBean.DataBean.CommentInfoBean norMarListBean) {
        if (norMarListBean != null && norMarListBean.getUser() != null) {
            if (norMarListBean.getUser().getId() != SpUtils.getInstance().getLoadUserBean().getData().getId()) {
                BusinessLayout.reportComment(mActivity, norMarListBean.getId(), norMarListBean.getUser().getId());
            }
        }
    }

    @Override
    public void onReportChildComment(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean) {
        if (replyCommentsBean != null && replyCommentsBean.getUser() != null) {
            if (replyCommentsBean.getUser().getId() != SpUtils.getInstance().getLoadUserBean().getData().getId()) {
                BusinessLayout.reportComment(mActivity, replyCommentsBean.getId(), replyCommentsBean.getUser().getId());
            } else {
                ToastUtils.showShort(mActivity.getApplicationContext(), "自己不能举报自己");
            }
        }
    }

    @Override
    public void onCopyCommentContent(CommentBean.DataBean.CommentInfoBean norMarListBean) {
        if (!TextUtils.isEmpty(norMarListBean.getContent())) {
            CommonUtil.contentClipboard(mActivity.getApplicationContext(), norMarListBean.getContent(), "复制成功");
        } else {
            ToastUtils.showShort(mActivity.getApplicationContext(), "复制出错或没有可复制内容～");
        }
    }


    /**
     * 查看更多
     */
    private void loadCommentMoreDetails(CommentBean.DataBean.CommentInfoBean norMarListBean, BaseCommentChildListAdapter adapter, ProgressBar progressBar, OnLookSurplusCommentListener listener) {

        SendRequest.getNewComment(SpUtils.getInstance().getLoadUserBean().getData().getToken(), adapter.pager, CommentEnum.COMMENT_CHILDREN.getCommentType(), norMarListBean.getId(), norMarListBean.getId(), new GenericsCallback<CommentBean>(new JsonGenericsSerializator()) {


            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showShort(mActivity.getApplicationContext(), "网络异常～");
                progressBar.setVisibility(View.GONE);
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(CommentBean response, int id) {
                progressBar.setVisibility(View.GONE);
                if (response != null && response.isSuccess()) {
                    List<CommentBean.DataBean.CommentInfoBean> mNorMarList = response.getData().getNormalList();
                    if (mNorMarList != null && mNorMarList.size() > 0) {
                        List<CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean> replyCommentList = new ArrayList<>();
                        for (CommentBean.DataBean.CommentInfoBean commentInfoBean : mNorMarList) {
                            CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean = new CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean();
                            replyCommentsBean.setCommentNum(commentInfoBean.getCommentNum());
                            replyCommentsBean.setContent(commentInfoBean.getContent());
                            replyCommentsBean.setCtime(commentInfoBean.getCtime());
                            replyCommentsBean.setCueCid(commentInfoBean.getCueCid());
                            replyCommentsBean.setCueContent(commentInfoBean.getCueContent());
                            replyCommentsBean.setId(commentInfoBean.getId());
                            replyCommentsBean.setPid(commentInfoBean.getPid());
                            replyCommentsBean.setPuid(commentInfoBean.getPuid());
                            replyCommentsBean.setPuname(commentInfoBean.getPuname());
                            replyCommentsBean.setSupportNum(commentInfoBean.getSupportNum());
                            replyCommentsBean.setSupported(commentInfoBean.isSupported());
                            replyCommentsBean.setTypeId(commentInfoBean.getTypeId());
                            replyCommentsBean.setType(commentInfoBean.getType());
                            replyCommentsBean.setUid(commentInfoBean.getUid());
                            replyCommentsBean.setUser(commentInfoBean.getUser());
                            replyCommentsBean.highlight = commentInfoBean.highlight;
                            replyCommentsBean.isAuthor = commentInfoBean.isAuthor;
                            replyCommentsBean.authorLike = commentInfoBean.authorLike;
                            replyCommentList.add(replyCommentsBean);
                        }
                        //Collections.reverse(replyCommentList);
                        if (adapter.pager == 1) {
                            adapter.setList(replyCommentList);
                        } else {
                            adapter.addList(replyCommentList);
                        }
                        listener.onLookSurplusComment(replyCommentList.size());
                        adapter.pager = adapter.pager + 1;
                    }
                }
            }
        });
    }


}
